package io.netty.example.http.websocketx.client.chatclient;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.example.http.websocketx.common.entity.NettyMessage;
import io.netty.example.http.websocketx.common.util.ByteNumberConverter;

import java.nio.charset.Charset;

/**
 * @Description
 * @Author fuguangli businessfgl@163.com
 * @Create date:    2017/9/26
 */
public class ChatClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    /**
     * <strong>Please keep in mind that this method will be renamed to
     * {@code messageReceived(ChannelHandlerContext, I)} in 5.0.</strong>
     * <p>
     *
     * @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
     *            belongs to
     * @param msg the message to handle
     * @throws Exception is thrown if an error occurred
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
//        byte[] head = new byte[4];
//        System.arraycopy(msg,0,head,0,4);
//        int bodyLen = ByteNumberConverter.byteArrayToInt(head);
//        byte[] body = new byte[bodyLen];
//        System.arraycopy(msg,4,body,0,bodyLen);

//        System.out.println(ByteNumberConverter.byteArrayToInt(head));
        System.out.println(msg.toString(Charset.forName("utf-8")));
    }
}
